const pool = require('./config')

class Db {
    // 静态方法
    static getInstance() {
      if (!Db.instance) {
        Db.instance = new Db();
      }
      return Db.instance;
    }
    // 构造函数
    constructor() {
      // 初始化连接对象
      this.dbClient = '';
    }
    //连接数据库
    connect() {
      let that = this;
      return new Promise((resolve, reject) => {
        if (!that.dbClient) {
          pool.getConnection(function(error, connection) {
              // 如果有错误就抛出
              if (error) {
                reject(error)
              } else {
                that.dbClient = connection
                resolve(that.dbClient);
              }
          })
        } else {
          resolve(that.dbClient);
        }
      })
    }

    // 处理数据
    query(sql, params = null) {
      let that = this;
      return new Promise((resolve, reject) => {
          this.connect().then(db => {
            db.query(sql, params, (error, results, fields) => {
                // 如果有错误就抛出
                if (error) {
                  reject(error)
                } else {
                  resolve(results)
                }
            })
            // 结束会话
            db.release();
            that.dbClient = ''
          })
      })
    }

    /**
   * 自定义查询
   * @param sql
   * @returns {Promise<any>}
   */
    queryStr(sql) {
      let that = this;
      return new Promise((resolve, reject) => {
          this.connect().then(db => {
            let query = db.query(sql, (error, results) => {
                // 如果有错误就抛出
                if (error) {
                  reject(error)
                } else {
                  resolve(results)
                }
            })
            console.log(query.sql);
            // 结束会话
            db.release();
            that.dbClient = ''
          })
      })
    }

    // 查询数据
    find(tableName,params={}) {
      return new Promise((resolve, reject) => {
        const sql = `select * from ${tableName} where ?`; 
        this.connect().then(db => {
          let query = db.query(sql, params, (error, results) => {
            // 如果有错误就抛出
            if (error) {
              reject(error)
            } else {
              resolve(results)
            }
          })
          console.log(query.sql);
          // 结束会话
          db.release();
          this.dbClient = ''
        })
      })
    }
    // 插入数据
    insert(tableName, params = {}) {
      return new Promise((resolve, reject) => {
        const sql = `INSERT INTO ${tableName} SET ?`;
        this.connect().then(db => {
          let query = db.query(sql, params, (error, results) => {
            // 如果有错误就抛出
            if (error) {
              reject(error)
            } else {
              resolve(results)
            }
          })
          console.log(query.sql);
          // 结束会话
          db.release();
          this.dbClient = ''
        })
      })
    }
    // 更新数据
    update(tableName, params = {}, whereJson={}) {
        return new Promise((resolve, reject) => {
          const sql = `UPDATE ${tableName} SET ? WHERE ?`
            this.connect().then(db => {
                let query = db.query(sql, [params, whereJson], (error, results) => {
                    // 如果有错误就抛出
                    if (error) {
                        reject(error)
                    } else {
                        resolve(results)
                    }
                })
                console.log(query.sql);
                // 结束会话
                db.release();
                this.dbClient = ''
            })
        })
    }
    // 删除数据
    delete(tableName, params = {}) {
        return new Promise((resolve, reject) => {
          const sql = `DELETE FROM ${tableName} WHERE ?`
            this.connect().then(db => {
               let query = db.query(sql, params, (error, results) => {
                    // 如果有错误就抛出
                    if (error) {
                        reject(error)
                    } else {
                        resolve(results)
                    }
                })
                console.log(query.sql);
                // 结束会话
                db.release();
                this.dbClient = ''
            })
        })
    }


}


module.exports = Db.getInstance();